/**
 * Created by Elsa on 2017/9/6.
 */

layui.define(['jquery', 'layer', 'tree'], function(exports){


    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var zNodes;
    $.ajax({
            type:"GET",
            url:contextPath+"/dept/",
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function (data) {
                // console.log(data.list);

                    zNodes = data.list;

                var setting = {
                    view: {
                        addHoverDom: addHoverDom,
                        removeHoverDom: removeHoverDom,
                        selectedMulti: false,
                        // fontCss: {'font-size':'20px','color':'black'}//统一设置样式
                    },
                    check: {
                        enable: false
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            pIdKey:"fjdid",
                            idKey:"bumenid"
                        }
                    },
                    edit: {
                        enable: false
                    },
                    callback: {
                        onClick: onClick,
                    }
                };
                // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);


                function addHoverDom(treeId, treeNode) {


                    var sObj = $("#" + treeNode.tId + "_span");

                    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;



                    if(getQueryVariable('C') == "false"||getQueryVariable('R') == "false"){
                        var addStr = "<span class='' id='addBtn_" + treeNode.tId
                            + "' title='添加节点' onfocus='this.blur();'></span>";
                    }else {
                        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
                            + "' title='添加节点' onfocus='this.blur();'></span>";
                    }
                    if(getQueryVariable('D') == "false"||getQueryVariable('R') == "false"){
                        var removeStr = "<span class='' id='removeBtn_" + treeNode.tId
                            + "' title='删除节点' onfocus='this.blur();'></span>";
                    }else {
                        var removeStr = "<span class='button remove' id='removeBtn_" + treeNode.tId
                            + "' title='删除节点' onfocus='this.blur();'></span>";
                    }
                    if(getQueryVariable('U') == "false"||getQueryVariable('R') == "false"){
                        var editStr = "<span class='' id='editBtn_" + treeNode.tId
                            + "' title='编辑节点' onfocus='this.blur();'></span>";
                    }else {
                        var editStr = "<span class='button edit' id='editBtn_" + treeNode.tId
                            + "' title='编辑节点' onfocus='this.blur();'></span>";
                    }



                    sObj.after(removeStr);
                    sObj.after(editStr);
                    sObj.after(addStr);

                    var btn = $("#addBtn_"+treeNode.tId);

                    if (btn) btn.bind("click", function(){
                        var zTree = $.fn.zTree.getZTreeObj("treeDemo");

                        layer.open({
                            cancel: function(index, layero){
                                var faIndex = index;
                                layer.confirm('确定要关闭么?未保存的信息将会丢失!', {icon: 3, title:'提示'}, function(index){
                                    //do something
                                    layer.close(index);
                                    layer.close(faIndex);
                                });
                                return false
                            },
                            type: 2,
                            title: '新增部门',
                            maxmin: true,
                            shadeClose: false, //点击遮罩关闭层
                            area : ['700px' , '520px'],
                            content: './newDept.html',
                            success:function(layero,index){
                                var body = layer.getChildFrame('body',index);
                                console.log( treeNode);
                                body.contents().find("[name='bumenid']").val(treeNode.bumenid);
                                body.contents().find("[name='fjdid']").val(treeNode.fjdid);
                            },
                            end:function(){
                                if($("#newDeptRes").val() == '1'){
                                    if($("#newDeptKind").val() == "同级部门"){
                                        zTree.addNodes(treeNode.getParentNode(), {bumenid:$("#newDeptId").val(), fjfid:$("#newDeptFjdid").val(), name:$("#newDeptName").val(),fzr:$("#newDeptfzr").val(),lxfs:$("#newDeptLxfs").val(),bmjj:$("#newDeptBmjj").val()});
                                    }else if($("#newDeptKind").val() == "下级部门"){
                                        zTree.addNodes(treeNode, {bumenid:$("#newDeptId").val(), fjfid:$("#newDeptFjdid").val(), name:$("#newDeptName").val(),fzr:$("#newDeptfzr").val(),lxfs:$("#newDeptLxfs").val(),bmjj:$("#newDeptBmjj").val()});
                                    }
                                }
                            }
                        });
                        return false;
                    });
//删除按钮事件
                    var btnRemove = $("#removeBtn_"+treeNode.tId);

                    if (btnRemove) btnRemove.bind("click", function(){
                        console.log(treeNode);
                        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                        layer.confirm('确定删除？', function(index){

                            layer.close(index);
                            //向服务端发送删除指令
                            // if(treeNode.fjdid == null){
                            //     layer.msg("唯一的根节点不能删除！")
                            // }else {
                                $.ajax({
                                    url: contextPath+"/dept/"+treeNode.bumenid,
                                    type: "DELETE",
                                    contentType:"application/json;charset=utf-8",
                                    dataType:"json",
                                    success: function(res) {
                                        if(res.message == "删除成功！"){
                                            zTree.removeNode(treeNode);
                                            layer.msg(res.message);
                                        }else {
                                            layer.msg(res.message);
                                        }

                                    }

                                });
                            // }

                        });
                        return false;
                    });
//编辑按钮事件
                    var btnEdit = $("#editBtn_"+treeNode.tId);

                    if (btnEdit) btnEdit.bind("click", function(){
                        var zTree = $.fn.zTree.getZTreeObj("treeDemo");

                        layer.open({
                            cancel: function(index, layero){
                                var body = layer.getChildFrame('body',index);
                                var faIndex = index;
                                console.log(layero);
                                if(!(body.contents().find("#modiSub").hasClass("layui-btn-disabled"))){

                                    layer.confirm('确定要关闭么?未保存的信息将会丢失!', {icon: 3, title:'提示'}, function(index){
                                        //do something
                                        layer.close(index);
                                        layer.close(faIndex);
                                    });
                                }else {
                                    layer.close(faIndex);
                                }

                                return false
                            },
                            type: 2,
                            title: '编辑部门',
                            maxmin: true,
                            shadeClose: false, //点击遮罩关闭层
                            area : ['700px' , '520px'],
                            content: './lookDept.html',
                            success:function(layero,index){
                                var body = layer.getChildFrame('body',index);
                                console.log( treeNode);
                                body.contents().find("[name='bumenid']").val(treeNode.bumenid);
                                body.contents().find("[name='fjdid']").val(treeNode.fjdid);
                                body.contents().find("[name='bmjj']").val(treeNode.bmjj);
                                body.contents().find("[name='name']").val(treeNode.name);
                                body.contents().find("[name='lxfs']").val(treeNode.lxfs);
                                body.contents().find("[name='fzr']").val(treeNode.fzr);
                            },
                            end:function(){
                                if($("#newDeptRes").val() == '1'){
                                    treeNode.name = $("#newDeptName").val();
                                    treeNode.fzr = $("#newDeptfzr").val();
                                    treeNode.bmjj = $("#newDeptBmjj").val();
                                    treeNode.lxfs = $("#newDeptLxfs").val();
                                    zTree.updateNode(treeNode);
                                    $("#newDeptRes").val("0");
                                }

                            }
                        });
                        return false;
                    });
                };
                function removeHoverDom(treeId, treeNode) {
                    $("#addBtn_"+treeNode.tId).unbind().remove();
                    $("#removeBtn_"+treeNode.tId).unbind().remove();
                    $("#editBtn_"+treeNode.tId).unbind().remove();
                };
                function onClick(e,treeId,treeNode) {
                    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                    if(getQueryVariable('R') == "false"){
                        return false;
                    }else {
                        layer.open({

                            type: 2,
                            title: '查看部门',
                            maxmin: true,
                            shadeClose: false, //点击遮罩关闭层
                            area : ['700px' , '520px'],
                            content: './lookDept2.html',
                            success:function(layero,index){
                                var body = layer.getChildFrame('body',index);
                                console.log( treeNode);
                                body.contents().find("[name='bumenid']").val(treeNode.bumenid);
                                body.contents().find("[name='fjdid']").val(treeNode.fjdid);
                                body.contents().find("[name='bmjj']").val(treeNode.bmjj);
                                body.contents().find("[name='name']").val(treeNode.name);
                                body.contents().find("[name='lxfs']").val(treeNode.lxfs);
                                body.contents().find("[name='fzr']").val(treeNode.fzr);
                            },
                            end:function(){
                                if($("#newDeptRes").val() == '1'){
                                    treeNode.name = $("#newDeptName").val();
                                    treeNode.fzr = $("#newDeptfzr").val();
                                    treeNode.bmjj = $("#newDeptBmjj").val();
                                    treeNode.lxfs = $("#newDeptLxfs").val();
                                    zTree.updateNode(treeNode);
                                    $("#newDeptRes").val("0");
                                }

                            }
                        });
                    }

                    return false;             }

            },
            error:function (e) {
            }
        });




    exports('RegMain2', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});